package com.ruoyi.project.tickets.controller.api;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysRole;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysDictDataService;
import com.ruoyi.project.tickets.domain.*;
import com.ruoyi.project.tickets.domain.constant.TicketsConstant;
import com.ruoyi.project.tickets.domain.eum.FrontEnum;
import com.ruoyi.project.tickets.domain.vo.FrontUserVo;
import com.ruoyi.project.tickets.domain.vo.SaleVo;
import com.ruoyi.project.tickets.domain.vo.SerialVo;
import com.ruoyi.project.tickets.domain.vo.StockVo;
import com.ruoyi.project.tickets.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 前端接口
 *
 * @author sunsahnwei
 * @date 2023年07月03日 13:22
 * @fileName IndexController
 */
@RestController
@RequestMapping("api/front/tickets")
public class IndexController extends BaseController {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private ISerialService serialService;
    @Autowired
    private ISaleService saleService;
    @Autowired
    private IStockService stockService;
    @Autowired
    private IShopUserService shopUserService;
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private IShopService shopService;

    /**
     * 个人详情
     *
     * @return
     */
    @GetMapping(value = "/my")
    public AjaxResult myIndex() {
        SysUser sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        Long id = shopUserService.getShopIdBySystemUser();
        Shop shop = shopService.selectShopById(id);
        FrontUserVo vo = new FrontUserVo();
        List<SysRole> roleList = sysUser.getRoles();
        List<String> list = roleList.stream().map(SysRole::getRoleName).collect(Collectors.toList());
        String stringFromList = String.join("|", list);
        vo.setRoleAddress(stringFromList);
        vo.setShopName(shop.getName());
        vo.setUserName(sysUser.getNickName());
        return AjaxResult.success(vo);
    }

    /**
     * 扫描二维码，票券详情
     *
     * @param serialNumber 扫描二维码后得到的票圈号
     * @return
     */
    @GetMapping(value = "selectBySerNumber/{serialNumber}")
    public AjaxResult selectBySerNumber(@PathVariable("serialNumber") String serialNumber) {
        return AjaxResult.success(serialService.selectBySerialNumber(serialNumber));
    }

    /**
     * 根据id核销票券
     *
     * @param id
     * @return
     */
    @GetMapping("/goOff/{id}")
    public AjaxResult goOff(@PathVariable Long id) {
        Serial serial = serialService.selectSerialById(id);
        if (TicketsConstant.IS_OFF.equals(serial.getIsOff())) {
            return AjaxResult.error("票券已核销！");
        }
        return toAjax(serialService.goOff(id));
    }

    /**
     * 核销列表
     *
     * @param
     * @return
     */
    @GetMapping("/seriallist")
    public TableDataInfo seriallist() {
        Serial serial = new Serial();
        serial.setOffUserId(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
        startPage();
        List<SerialVo> list = serialService.selectSerialList(serial);
        return getDataTable(list);
    }

    /**
     * 销售列表
     *
     * @param
     * @return
     */
    @GetMapping("/saleList")
    public TableDataInfo saleList() {
        Sale sale = new Sale();
        sale.setUserId(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
        startPage();
        List<Sale> list = saleService.selectList(sale);
        return getDataTable(list);
    }

    /**
     * 销售详情
     *
     * @param id id
     * @return
     */
    @GetMapping("/saleList/{id}")
    public AjaxResult saleList(@PathVariable Long id) {
        Sale details = new Sale();
        details.setId(id);
        List<SaleVo> list = saleService.selectSaleList(details);
        return AjaxResult.success(list);
    }

    /**
     * 库存列表
     *
     * @param
     * @return
     */
    @GetMapping("/stocklist")
    public TableDataInfo list() {
        startPage();
        List<StockVo> list = stockService.selectStockList(new Stock());
        return getDataTable(list);
    }

    /**
     * 数据字典
     *
     * @return
     */
    @GetMapping(value = "/dictType/{type}")
    public AjaxResult dictType(@PathVariable String type) {
        String value = null;
        FrontEnum[] frontEnum = FrontEnum.values();
        for (FrontEnum front : frontEnum) {
            if (type.equals(front.getValue())) {
                value = front.getDesp();
            }
        }

        return AjaxResult.success(dictDataService.selectDictDataByType(value));
    }
}
